package com.vay.de.dang.online.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;

import com.vay.de.dang.online.R;

/**
 * ProjectName: NsVnm
 * Author     : 南山
 * Date       : 2023/12/14
 * Describe   : 类描述
 */
public class SendCodeView extends androidx.appcompat.widget.AppCompatTextView {

    private final long MaxTime = 60 * 1000;
    private Handler handler = new Handler();
    private long lastTime;

    public SendCodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void startCountDown() {
        setClickable(false);
        setBackgroundResource(R.drawable.bg_bnt_apply_none);
        lastTime = System.currentTimeMillis();
        handler.postDelayed(timer, 0);
    }

    private Runnable timer = new Runnable() {

        @Override
        public void run() {
            long time = MaxTime - (System.currentTimeMillis() - lastTime);
            if (time <= 0) {
                setBackgroundResource(R.drawable.bg_bnt_apply);
                setText(getResources().getString(R.string.bnt_send));
                setClickable(true);
                handler.removeCallbacks(timer);
            } else {
                setClickable(false);
                setText((time / 1000) + "s");
                handler.postDelayed(timer, 1000);
            }
        }
    };
}
